﻿using DeviceMonitor.ViewModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DeviceMonitor.Controls
{
    /// <summary>
    /// DeviceControl.xaml 的交互逻辑
    /// </summary>
    public partial class DeviceControl : UserControl
    {
        String alerm = "";
        public DeviceControl()
        {
            InitializeComponent();
        }

        [BindableAttribute(true)]
        public ItemViewModel ItemData
        {
            get { return (ItemViewModel)GetValue(ItemDataProperty); }
            set { SetValue(ItemDataProperty, value); }
        }
        public static ItemViewModel GetItemData(DependencyObject obj)
        {
            return (ItemViewModel)obj.GetValue(ItemDataProperty);
        }
        public static void SetItemData(DependencyObject obj, ItemViewModel value)
        {
            obj.SetValue(ItemDataProperty, value);
        }

        public static readonly DependencyProperty ItemDataProperty = DependencyProperty.Register("ItemData", typeof(ItemViewModel), typeof(DeviceControl), new PropertyMetadata(new PropertyChangedCallback(OnItemChange)));

        static private void OnItemChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as DeviceControl;
        }

        [BindableAttribute(true)]
        public string Time
        {
            get { return (string)GetValue(TimeProperty); }
            set
            {
                SetValue(TimeProperty, value);
            }
        }
        public static string GetTime(DependencyObject obj)
        {
            return (string)obj.GetValue(TimeProperty);
        }
        public static void SetTime(DependencyObject obj, string value)
        {
            obj.SetValue(TimeProperty, value);
        }
        public static readonly DependencyProperty TimeProperty = DependencyProperty.Register("Time", typeof(string), typeof(DeviceControl), new PropertyMetadata(new PropertyChangedCallback(OnTimeChange)));
        static void OnTimeChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as DeviceControl;
            control.StoryboardPlay();

            if(!control.alerm.Equals(control.ItemData.Alerm))
            {
                control.alerm = (control.ItemData.Alerm+"").PadLeft(8,'0');
                control.dealAlerm(control.alerm);
            }

            try
            {
                control.calcRate(control, double.Parse(control.ItemData.ShouldNum), double.Parse(control.ItemData.ActNum));
            }
            catch { }
        }
        protected void StoryboardPlay()
        {
            /*
            DoubleAnimation da = new DoubleAnimation();
            da.From = 1;
            da.To = 0.2;
            da.Duration = new Duration(TimeSpan.Parse("0:0:1"));
            topBox.BeginAnimation(Ellipse.OpacityProperty, da);
            */
            ColorAnimation animation01 = new ColorAnimation();
            animation01.From = Colors.Black;
            animation01.To = Colors.Green;
            animation01.Duration = new Duration(TimeSpan.Parse("0:0:1"));
            topColorBrush.BeginAnimation(SolidColorBrush.ColorProperty, animation01);

            ColorAnimation animation02 = new ColorAnimation();
            animation02.From = Colors.Green;
            animation02.To = Colors.Black;
            animation02.Duration = new Duration(TimeSpan.Parse("0:0:1"));
            topColorBrush.BeginAnimation(SolidColorBrush.ColorProperty, animation02);
            /*
            DoubleAnimation da2 = new DoubleAnimation();
            da2.From = 0.2;
            da2.To = 1;
            da2.Duration = new Duration(TimeSpan.Parse("0:0:1"));
            topBox.BeginAnimation(Ellipse.OpacityProperty, da2);
            */
        }
        private void calcRate(DeviceControl mPanel, double shouldNum, double actNum)
        {
            double nRate = 0;
            if (shouldNum == 0)
            {
                nRate = 0.0;
            }
            else
            {
                if (actNum >= shouldNum)
                {
                    nRate = 1.0;
                }
                else
                {
                    nRate = (double)actNum / shouldNum;
                }
            }
            double dblRate = rateBasePanel.ActualWidth * nRate;
            int rateWidth = Convert.ToInt32(dblRate.ToString("0"));
            ratePanel.Width = rateWidth;

            textRate.Text = (nRate*100).ToString("0.00") + "%";
        }
        private void dealAlerm(String alermString)
        {
            //setAlermColor(Aler01, alermString.Substring(0, 1));
            //setAlermColor(Aler02, alermString.Substring(1, 1));
            //setAlermColor(Aler03, alermString.Substring(2, 1));
            //setAlermColor(Aler04, alermString.Substring(3, 1));
            //setAlermColor(Aler05, alermString.Substring(4, 1));
            //setAlermColor(Aler06, alermString.Substring(5, 1));
            //setAlermColor(Aler07, alermString.Substring(6, 1));
            //setAlermColor(Aler08, alermString.Substring(7, 1));
            setAlermColor(Aler01,alermString.Substring(7,1));
            setAlermColor(Aler02,alermString.Substring(6,1));
            setAlermColor(Aler03,alermString.Substring(5,1));
            setAlermColor(Aler04,alermString.Substring(4,1));
            setAlermColor(Aler05,alermString.Substring(3,1));
            setAlermColor(Aler06,alermString.Substring(2,1));
            setAlermColor(Aler07,alermString.Substring(1,1));
            setAlermColor(Aler08,alermString.Substring(0,1));

        }
        private void setAlermColor(Border alermPanel,String val)
        {
            if ("1".Equals(val))
            {
                alermPanel.Background = Brushes.Red;
            }
            else
            {
                alermPanel.Background = Brushes.Green;
            }
        }
    }
}
